﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using helloworld2;

namespace helloworld2
{
    class CityContainer
    {
        
        private static CityContainer instance=null;

        private CityContainer()
        {
        }

        public static CityContainer Instance
        {
            get
            {
                if (instance==null)
                {
                    instance = new CityContainer();
                }
                return instance;
            }
        }

        List<City> cities = new List<City>();

        public void addCity(City newCity)
        {
            cities.Add(newCity);
        }

        public City findCity(string cityName)
        {
            City c = null;
            for (int i = 0; i < cities.Count; i++)
            {
                if (cities[i].name == cityName)
                {
                    c = cities[i];
                }
            }
            return c;
        }

        public City findCityByCoordinates(KeyValuePair<int,int> yx)
        {
            City c = null;
            for (int i = 0; i < cities.Count; i++)
            {
                KeyValuePair<int, int> cityCoordinates = cities[i].getCoordinates();

                if (cityCoordinates.Key == yx.Key && cityCoordinates.Value == yx.Value)
                {
                    c = cities[i];
                }
            }
            return c;
        }

        public List<City> getCities()
        {
            var cityList = new List<City>();
            foreach (City c in cities)
            {
                cityList.Add(c);
            }
            return cityList;
        }

        public City getCityByCoordinates(KeyValuePair<int, int> coordinates)
        {
            City c = new City();
            for (int i = 0; i < cities.Count; i++)
            {
                c = cities[i];
                if (c.lookForCoordinates(coordinates))
                {
                    i = cities.Count;
                }
            }
            return c;
        }


    }
}
